<template>
  <div class="login">
    <el-form ref="form" class="form" :model="form" :rules="rules">
      <div class="logo">
        <img src="@/assets/common/login-logo.png" alt="">
      </div>
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          type="password"
        /></el-form-item>
      <el-form-item>
        <el-button class="btn" type="primary" @click="btnClick">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: '',
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('手机号错误'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6~12位密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 点击登录
    btnClick() {
      // 验证表单
      this.$refs.form.validate(async(res) => {
        if (res) {
          await this.$store.dispatch('user/toLogin', this.form)
          this.$message.success('登录成功')
          const redirect = this.$route.query.redirect || '/'
          this.$router.push(redirect)
        } else {
          this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    width: 450px;

    .logo {
      text-align: center;
      padding-bottom: 20px;
    }
    .btn {
      width: 100%;
      height: 50px;
    }
  }
}
</style>
